/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PantallaPrincipal.java
 *
 * Created on 01/09/2011, 18:53:47
 */
package ui;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import modbus.ObservadorMensajes;
import modbus.SuscriptorMensajes;

/**
 *
 * @author Leandro
 */
public class PantallaPrincipal extends javax.swing.JFrame implements ObservadorMensajes {

    /** Creates new form PantallaPrincipal */
    public PantallaPrincipal() {

        initComponents();
    }
    private ComboBoxModel puertos;

    public PantallaPrincipal(Controlador c) {
        this.c = c;
        puertos = new DefaultComboBoxModel(c.getPorts());
        initComponents();
        SuscriptorMensajes.getI().addObserver(this);
    }
    private Controlador c;
    private int[] valores;

    public int[] getValores() {
        return valores;
    }

    public void setValores(int[] valores) {
        this.valores = valores;
    }

    public Controlador getC() {
        return c;
    }

    public void setC(Controlador c) {
        this.c = c;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfIDDisp = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfDirInicio = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfCantReg = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        tfValorWrite = new javax.swing.JTextField();
        cbFuncion = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        consola = new javax.swing.JTextArea();
        jLabel12 = new javax.swing.JLabel();
        tfRefresco = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        botStart = new javax.swing.JButton();
        botStop = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        campoVariables = new javax.swing.JTextArea();
        jSeparator2 = new javax.swing.JSeparator();
        tabPannel = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        tfVelocidad = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        cbParidad = new javax.swing.JComboBox();
        cbPalabra = new javax.swing.JComboBox();
        cbParada = new javax.swing.JComboBox();
        comboPuertos = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel17 = new javax.swing.JLabel();
        tfDirIP = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        tfPuertoIP = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        botConectar = new javax.swing.JButton();
        botDesConectar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jPanel5 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        tfCantR = new javax.swing.JTextField();
        tftimeOut = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel4.setText("ID Dispositivo:");

        jLabel5.setText("Función:");

        tfIDDisp.setText("1");
        tfIDDisp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfIDDispActionPerformed(evt);
            }
        });
        tfIDDisp.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfIDDispFocusLost(evt);
            }
        });

        jLabel6.setText("Dirección Inicio:");

        tfDirInicio.setText("0");
        tfDirInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfDirInicioActionPerformed(evt);
            }
        });
        tfDirInicio.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfDirInicioFocusLost(evt);
            }
        });

        jLabel7.setText("Cant. Registros:");

        tfCantReg.setText("1");
        tfCantReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCantRegActionPerformed(evt);
            }
        });
        tfCantReg.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfCantRegFocusLost(evt);
            }
        });

        jLabel14.setText("Valor a escribir (funcion 06):");

        tfValorWrite.setText("1");
        tfValorWrite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfValorWriteActionPerformed(evt);
            }
        });
        tfValorWrite.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfValorWriteFocusLost(evt);
            }
        });

        cbFuncion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "3", "6", "16" }));
        cbFuncion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFuncionActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Configuracion del Dispositivo");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfDirInicio, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tfIDDisp, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbFuncion, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfValorWrite, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfCantReg, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(323, 323, 323))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addContainerGap(585, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(tfIDDisp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbFuncion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(tfValorWrite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tfDirInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(tfCantReg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        consola.setColumns(20);
        consola.setRows(5);
        jScrollPane1.setViewportView(consola);

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel12.setText("Pantalla de Escaneo");

        tfRefresco.setText("3000");
        tfRefresco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfRefrescoActionPerformed(evt);
            }
        });

        jLabel13.setText("Intervalo[ms]:");

        botStart.setText("Start");
        botStart.setEnabled(false);
        botStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botStartActionPerformed(evt);
            }
        });

        botStop.setText("Stop");
        botStop.setEnabled(false);
        botStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botStopActionPerformed(evt);
            }
        });

        jButton1.setText("Limpiar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hexadecimal", "Decimal" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        campoVariables.setColumns(20);
        campoVariables.setRows(5);
        jScrollPane2.setViewportView(campoVariables);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(botStart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tfRefresco, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(botStop, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1))
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(tfRefresco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botStart)
                    .addComponent(botStop)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(67, 67, 67))
        );

        tabPannel.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabPannelStateChanged(evt);
            }
        });

        jLabel2.setText("Velocidad:");

        jLabel1.setText("Puerto:");

        tfVelocidad.setText("9600");

        jLabel9.setText("Palabra:");

        jLabel10.setText("Paridad:");

        jLabel11.setText("Bits Parada:");

        cbParidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NONE", "ODD", "EVEN" }));
        cbParidad.setToolTipText("");
        cbParidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbParidadActionPerformed(evt);
            }
        });

        cbPalabra.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "8", "7", "6", "5" }));

        cbParada.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2" }));

        comboPuertos.setModel(puertos);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfVelocidad))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(comboPuertos, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbParidad, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbPalabra, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbParada, 0, 64, Short.MAX_VALUE))
                .addGap(146, 146, 146))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboPuertos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbPalabra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfVelocidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(cbParidad, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel11)
                    .addComponent(cbParada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        tabPannel.addTab("Serie", jPanel1);

        jLabel17.setText("Dirección IP:");

        tfDirIP.setText("127.0.0.1");
        tfDirIP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfDirIPActionPerformed(evt);
            }
        });

        jLabel18.setText("Dirección IP:");

        tfPuertoIP.setText("502");
        tfPuertoIP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfPuertoIPActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfDirIP, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel18)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfPuertoIP, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(373, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(tfDirIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(tfPuertoIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        tabPannel.addTab("TCP", jPanel4);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Configuración de Conexion");

        botConectar.setText("Conectar");
        botConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botConectarActionPerformed(evt);
            }
        });

        botDesConectar.setText("Desconectar");
        botDesConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botDesConectarActionPerformed(evt);
            }
        });

        jLabel15.setText("Cantidad Reintentos:");

        tfCantR.setText("3");

        tftimeOut.setText("1000");

        jLabel16.setText("TimeOut:");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                        .addComponent(tfCantR, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 105, Short.MAX_VALUE)
                        .addComponent(tftimeOut, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(tfCantR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel16)
                    .addComponent(tftimeOut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botConectar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 565, Short.MAX_VALUE)
                        .addComponent(botDesConectar, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tabPannel, javax.swing.GroupLayout.PREFERRED_SIZE, 549, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 741, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 746, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(21, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tabPannel, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botConectar)
                    .addComponent(botDesConectar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(426, Short.MAX_VALUE)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(398, 398, 398)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botConectarActionPerformed
        switch(this.tabPannel.getSelectedIndex()){
//        this.c.conect(comboPuertos.getSelectedItem().toString(),Integer.parseInt(tfVelocidad.getText()),Integer.parseInt(cbPalabra.getSelectedItem().toString()),Integer.parseInt(cbParada.getSelectedItem().toString()),cbParidad.getSelectedItem().toString());
            case 0:
                this.c.conect(comboPuertos.getSelectedItem().toString(), Integer.parseInt(tfVelocidad.getText()), Integer.parseInt(cbPalabra.getSelectedItem().toString()), Integer.parseInt(cbParada.getSelectedItem().toString()), cbParidad.getSelectedItem().toString(), Integer.parseInt(tftimeOut.getText()), Integer.parseInt(tfCantR.getText()));
                this.botStart.setEnabled(true);
                this.botStop.setEnabled(true);
            break;
            case 1:
                this.c.conect(tfDirIP.getText(),Integer.parseInt(tfPuertoIP.getText()),Integer.parseInt(tftimeOut.getText()),Integer.parseInt(tfCantR.getText()));
                this.botStart.setEnabled(true);
                this.botStop.setEnabled(true);
            break;
        }
    }//GEN-LAST:event_botConectarActionPerformed

    private void botStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botStartActionPerformed
        //Inicia el hilo de transmisión/recepción
        c.start(Integer.parseInt(tfIDDisp.getText()),
                Integer.parseInt(cbFuncion.getSelectedItem().toString()),
                Integer.parseInt(tfDirInicio.getText()),
                Integer.parseInt(tfCantReg.getText()),
                Integer.parseInt(tfValorWrite.getText()),
                this.valores);
    }//GEN-LAST:event_botStartActionPerformed

    private void botStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botStopActionPerformed
        //Detiene el hilo
        c.stop();
    }//GEN-LAST:event_botStopActionPerformed

private void botDesConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botDesConectarActionPerformed
    // TODO add your handling code here:
    c.disconect();
    this.botStart.setEnabled(false);
    this.botStop.setEnabled(false);
}//GEN-LAST:event_botDesConectarActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    consola.setText("");
    campoVariables.setText("");
}//GEN-LAST:event_jButton1ActionPerformed

private void tfDirInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfDirInicioActionPerformed
    // TODO add your handling code here:
    c.stop();
//    c.start(Integer.parseInt(tfIDDisp.getText()),
//                Integer.parseInt(cbFuncion.getSelectedItem().toString()),
//                Integer.parseInt(tfDirInicio.getText()),
//                Integer.parseInt(tfCantReg.getText()),
//                Integer.parseInt(tfValorWrite.getText()),
//                this.valores);
}//GEN-LAST:event_tfDirInicioActionPerformed

private void cbParidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbParidadActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_cbParidadActionPerformed

private void cbFuncionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFuncionActionPerformed
    // TODO add your handling code here:
    c.stop();

}//GEN-LAST:event_cbFuncionActionPerformed

private void tfCantRegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfCantRegActionPerformed
    // TODO add your handling code here:
    if (cbFuncion.getSelectedItem().toString().equals("16")) {
        new CargaDatosMultiples(Integer.parseInt(tfCantReg.getText()), this).setVisible(true);
    }
    c.stop();
//    c.start(Integer.parseInt(tfIDDisp.getText()),
//                Integer.parseInt(cbFuncion.getSelectedItem().toString()),
//                Integer.parseInt(tfDirInicio.getText()),
//                Integer.parseInt(tfCantReg.getText()),
//                Integer.parseInt(tfValorWrite.getText()),
//                this.valores);
}//GEN-LAST:event_tfCantRegActionPerformed

private void tfCantRegFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfCantRegFocusLost
    // TODO add your handling code here:
    if (cbFuncion.getSelectedItem().toString().equals("16")) {
        new CargaDatosMultiples(Integer.parseInt(tfCantReg.getText()), this).setVisible(true);
    }
    c.stop();
//    c.start(Integer.parseInt(tfIDDisp.getText()),
//                Integer.parseInt(cbFuncion.getSelectedItem().toString()),
//                Integer.parseInt(tfDirInicio.getText()),
//                Integer.parseInt(tfCantReg.getText()),
//                Integer.parseInt(tfValorWrite.getText()),
//                this.valores);
}//GEN-LAST:event_tfCantRegFocusLost

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
    // TODO add your handling code here:
    Controlador.setSistemaNum(jComboBox1.getSelectedItem().toString().charAt(0));
}//GEN-LAST:event_jComboBox1ActionPerformed

private void tfValorWriteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfValorWriteActionPerformed
    // TODO add your handling code here:
    c.stop();
//    c.start(Integer.parseInt(tfIDDisp.getText()),
//                Integer.parseInt(cbFuncion.getSelectedItem().toString()),
//                Integer.parseInt(tfDirInicio.getText()),
//                Integer.parseInt(tfCantReg.getText()),
//                Integer.parseInt(tfValorWrite.getText()),
//                this.valores);
}//GEN-LAST:event_tfValorWriteActionPerformed

private void tfValorWriteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfValorWriteFocusLost
    // TODO add your handling code here:
}//GEN-LAST:event_tfValorWriteFocusLost

private void tfIDDispActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfIDDispActionPerformed
    // TODO add your handling code here:
    c.stop();
//    c.start(Integer.parseInt(tfIDDisp.getText()),
//                Integer.parseInt(cbFuncion.getSelectedItem().toString()),
//                Integer.parseInt(tfDirInicio.getText()),
//                Integer.parseInt(tfCantReg.getText()),
//                Integer.parseInt(tfValorWrite.getText()),
//                this.valores);
}//GEN-LAST:event_tfIDDispActionPerformed

private void tfIDDispFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfIDDispFocusLost
    // TODO add your handling code here:
}//GEN-LAST:event_tfIDDispFocusLost

private void tfDirInicioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfDirInicioFocusLost
    // TODO add your handling code here:
}//GEN-LAST:event_tfDirInicioFocusLost

private void tfRefrescoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfRefrescoActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_tfRefrescoActionPerformed

private void tfDirIPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfDirIPActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_tfDirIPActionPerformed

private void tabPannelStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabPannelStateChanged
    // TODO add your handling code here:
    System.out.println("Panel"+this.tabPannel.getSelectedIndex());
}//GEN-LAST:event_tabPannelStateChanged

private void tfPuertoIPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfPuertoIPActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_tfPuertoIPActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botConectar;
    private javax.swing.JButton botDesConectar;
    private javax.swing.JButton botStart;
    private javax.swing.JButton botStop;
    private javax.swing.JTextArea campoVariables;
    private javax.swing.JComboBox cbFuncion;
    private javax.swing.JComboBox cbPalabra;
    private javax.swing.JComboBox cbParada;
    private javax.swing.JComboBox cbParidad;
    private javax.swing.JComboBox comboPuertos;
    private javax.swing.JTextArea consola;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTabbedPane tabPannel;
    private javax.swing.JTextField tfCantR;
    private javax.swing.JTextField tfCantReg;
    private javax.swing.JTextField tfDirIP;
    private javax.swing.JTextField tfDirInicio;
    private javax.swing.JTextField tfIDDisp;
    private javax.swing.JTextField tfPuertoIP;
    private javax.swing.JTextField tfRefresco;
    private javax.swing.JTextField tfValorWrite;
    private javax.swing.JTextField tfVelocidad;
    private javax.swing.JTextField tftimeOut;
    // End of variables declaration//GEN-END:variables

    public void actualizar(String b) {
        consola.setText(b + "\n" + consola.getText());

    }

    public void actualizarVariables(byte[] b) {
        int var = 0;
        for (int i = 0; i < b.length; i += 2) {
            if (Controlador.getSistemaNum() == 'H') {
                campoVariables.setText("Variable " + var + ": " + Integer.toHexString(0x000000FF & b[i]) + " " + Integer.toHexString(0x000000FF & b[i + 1]) + "\n" + campoVariables.getText());
            } else if (Controlador.getSistemaNum() == 'D') {
                campoVariables.setText("Variable " + var + ": " + b[i] + " " + b[i + 1] + "\n" + campoVariables.getText());
            }

            var++;
        }
    }
}
